<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <meta name="author" content="隐星魂 (Roy.Sun)">
        
        <link rel="shortcut icon" href="../../../img/favicon.ico">
        <title>架构移植笔记 - 玄武操作系统</title>
        <link href="../../../css/bootstrap.min.css" rel="stylesheet">
        <link href="../../../css/font-awesome.min.css" rel="stylesheet">
        <link href="../../../css/base.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.3/styles/color-brewer.min.css">

        <script src="../../../js/jquery-1.10.2.min.js" defer></script>
        <script src="../../../js/bootstrap.min.js" defer></script>
        <script src="https://cdn.staticfile.org/highlight.js/10.0.3/highlight.min.js"></script>
        <script>hljs.initHighlightingOnLoad();</script> 
    </head>

    <body>
        <div class="navbar fixed-top navbar-expand-lg navbar-dark bg-primary">
            <div class="container">
                <a class="navbar-brand" href="../../..">玄武操作系统</a>
                <!-- Expander button -->
                <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbar-collapse">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <!-- Expanded navigation -->
                <div id="navbar-collapse" class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li class="navitem">
                                <a href="../../.." class="nav-link">主页</a>
                            </li>
                            <li class="navitem">
                                <a href="../../../license/" class="nav-link">开源协议</a>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">快速开发指南 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">STM32</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../../QuickGuide/stm32/01-Envsetup-Guide/" class="dropdown-item">开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/stm32/02-Project-Setup-Guide/" class="dropdown-item">工程配置指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/stm32/03-atkh743/" class="dropdown-item">正点原子阿波罗H743开发板上手指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/stm32/04-fk429m/" class="dropdown-item">反客F429核心板上手指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/stm32/05-atkf407core/" class="dropdown-item">正点原子F407核心板上手指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/stm32/06-atkf103core/" class="dropdown-item">正点原子F103核心板上手指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">NXP S32K</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../../QuickGuide/s32k/01-Envsetup-Guide/" class="dropdown-item">开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/s32k/02-Project-Setup-Guide/" class="dropdown-item">工程配置指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">玄武内核模块</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../../QuickGuide/xwko/01-XuanWuKO-Guide/" class="dropdown-item">上手指南</a>
</li>
    </ul>
  </li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">应用手册 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../../../UserManual/00-index/" class="dropdown-item">00.目录</a>
</li>
                                    
<li>
    <a href="../../../UserManual/01-brief/" class="dropdown-item">01.简介</a>
</li>
                                    
<li>
    <a href="../../../UserManual/02-Basic-System-Architecture/" class="dropdown-item">02.系统基本架构</a>
</li>
                                    
<li>
    <a href="../../../UserManual/03-Build-System/" class="dropdown-item">03.构建系统</a>
</li>
                                    
<li>
    <a href="../../../UserManual/04-Boot-Flow/" class="dropdown-item">04.启动流程</a>
</li>
                                    
<li>
    <a href="../../../UserManual/05-IRQ/" class="dropdown-item">05.中断控制</a>
</li>
                                    
<li>
    <a href="../../../UserManual/06-SKD/" class="dropdown-item">06.调度器</a>
</li>
                                    
<li>
    <a href="../../../UserManual/07-Thread/" class="dropdown-item">07.线程</a>
</li>
                                    
<li>
    <a href="../../../UserManual/08-SWT/" class="dropdown-item">08.软件定时器</a>
</li>
                                    
<li>
    <a href="../../../UserManual/09-PM/" class="dropdown-item">09.电源管理</a>
</li>
                                    
<li>
    <a href="../../../UserManual/10-Lock/" class="dropdown-item">10.锁</a>
</li>
                                    
<li>
    <a href="../../../UserManual/11-Sync/" class="dropdown-item">11.同步</a>
</li>
                                    
<li>
    <a href="../../../UserManual/12-MM/" class="dropdown-item">12.内存管理</a>
</li>
                                    
<li>
    <a href="../../../UserManual/13-C-Lib/" class="dropdown-item">13.玄武C库</a>
</li>
                                </ul>
                            </li>
                            <li class="dropdown active">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">移植笔记 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../../00-index/" class="dropdown-item">目录</a>
</li>
                                    
<li>
    <a href="../../01-Porting-Note/" class="dropdown-item">移植笔记</a>
</li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">ARM-Cortex-M</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="./" class="dropdown-item active">架构移植笔记</a>
</li>
            
<li>
    <a href="../02-STM32-Porting-Note/" class="dropdown-item">STM32</a>
</li>
    </ul>
  </li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">规范 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../../../Specification/00-index/" class="dropdown-item">目录</a>
</li>
                                    
<li>
    <a href="../../../Specification/01-Git-Commit-Specification/" class="dropdown-item">git-commit规范</a>
</li>
                                    
<li>
    <a href="../../../Specification/02-Version-Branch-Specification/" class="dropdown-item">版本分支规范</a>
</li>
                                </ul>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav ml-auto">
                        <li class="nav-item">
                            <a href="#" class="nav-link" data-toggle="modal" data-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> 搜索
                            </a>
                        </li>
                            <li class="nav-item">
                                <a rel="prev" href="../../01-Porting-Note/" class="nav-link">
                                    <i class="fa fa-arrow-left"></i> 上一篇
                                </a>
                            </li>
                            <li class="nav-item">
                                <a rel="next" href="../02-STM32-Porting-Note/" class="nav-link">
                                    下一篇 <i class="fa fa-arrow-right"></i>
                                </a>
                            </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
            <div class="row">
                    <div class="col-md-3"><div class="navbar-light navbar-expand-md bs-sidebar hidden-print affix" role="complementary">
    <div class="navbar-header">
        <button type="button" class="navbar-toggler collapsed" data-toggle="collapse" data-target="#toc-collapse" title="Table of Contents">
            <span class="fa fa-angle-down"></span>
        </button>
    </div>

    
    <div id="toc-collapse" class="navbar-collapse collapse card bg-secondary">
        <ul class="nav flex-column">
            
            <li class="nav-item" data-level="1"><a href="#arm-cortex-m" class="nav-link">ARM-Cortex-M移植说明</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            
            <li class="nav-item" data-level="1"><a href="#1" class="nav-link">1.概述</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            
            <li class="nav-item" data-level="1"><a href="#2" class="nav-link">2.栈</a>
              <ul class="nav flex-column">
            <li class="nav-item" data-level="2"><a href="#_1" class="nav-link">栈结构</a>
              <ul class="nav flex-column">
              </ul>
            </li>
              </ul>
            </li>
        </ul>
    </div>
</div></div>
                    <div class="col-md-9" role="main">

<h1 id="arm-cortex-m">ARM-Cortex-M移植说明</h1>
<hr />
<h1 id="1">1.概述</h1>
<p>玄武OS的源码中，已包含了大部分ARM-Cortex-M架构的代码：</p>
<ul>
<li>架构描述层(ADL)<ul>
<li>ARMv7m:xwcd/soc/arm/v7m/gcc</li>
<li>ARMv6m:xwcd/soc/arm/v6m/gcc</li>
</ul>
</li>
<li>CPU描述层(CDL)<ul>
<li>m0:xwcd/soc/arm/v6m/gcc/m0</li>
<li>m0p:xwcd/soc/arm/v6m/gcc/m0p</li>
<li>m3:xwcd/soc/arm/v7m/gcc/m3</li>
<li>m4:xwcd/soc/arm/v7m/gcc/m4</li>
<li>m7:xwcd/soc/arm/v7m/gcc/m7</li>
</ul>
</li>
<li>SOC描述层(SDL)<ul>
<li>STM32:<ul>
<li>L0/F0:xwcd/soc/arm/v7m/gcc/m0/stm32</li>
<li>L1/F1:xwcd/soc/arm/v7m/gcc/m3/stm32</li>
<li>L4/F4:xwcd/soc/arm/v7m/gcc/m4/stm32</li>
<li>F7/H7:xwcd/soc/arm/v7m/gcc/m7/stm32</li>
</ul>
</li>
<li>S32K:xwcd/soc/arm/v7m/gcc/m4/s32k14x</li>
<li>i.MX RT1052:xwcd/soc/arm/v7m/gcc/m7/mimxrt1052</li>
</ul>
</li>
</ul>
<p>新的SOC芯片，只需在CDL文件夹中增加SOC文件夹，其中包括：</p>
<ul>
<li><code>soc_lowlevel_init()</code>：低级初始化，此时内存还未初始化，不可访问全局变量；<ul>
<li>SOC的其他低级初始化</li>
</ul>
</li>
<li><code>soc_init()</code><ul>
<li>初始化中断控制器与调度器<ul>
<li>多核系统：<ul>
<li><code>xwmp_irqc_construct()</code>：初始化每个CPU的中断控制器；</li>
<li><code>xwmp_irqc_register()</code>：将每个CPU的中断控制器注册到中断控制子系统；</li>
<li><code>xwmp_skd_init_lc()</code>：分别在每个CPU上都运行一次这个函数初始化自己的调度器；</li>
</ul>
</li>
<li>单核系统：<ul>
<li><code>xwup_irqc_init()</code>：初始化中断控制器；</li>
<li><code>xwup_skd_init_lc()</code>：初始化调度器；</li>
</ul>
</li>
</ul>
</li>
<li>SOC的其他初始化</li>
</ul>
</li>
<li>玄武OS内核移植层(XWOSPL)的实现<ul>
<li>中断：ospl_irq_impl.h/ospl_irq_impl.c</li>
<li>调度器：ospl_skd_impl.h/ospl_skd_impl.c</li>
<li>硬件定时器：ospl_syshwt_impl.h/ospl_syshwt_impl.c</li>
</ul>
</li>
<li>*.lds：链接脚本</li>
</ul>
<p>可以复制一个SDL文件夹（例如STM32）作为模板，所有Cortex-M的单片机XWOSPL的实现都是一样。</p>
<h1 id="2">2.栈</h1>
<ul>
<li>玄武OS在ARMv6/7-m平台的实现上，使用<strong>8字节对齐</strong>的栈，
    因此必须在芯片初始化的最早阶段将CCR寄存器中的STKALIGN设置为1：</li>
</ul>
<pre><code class="C">cm_scs.scb.ccr.bit.stkalign = 1;
</code></pre>

<ul>
<li>
<p>使用满递减栈，即配置文件<code>cfg/xwos.h</code>中<code>XWMMCFG_FD_STACK</code>配置为1：</p>
</li>
<li>
<p>当CPU中包含浮点单元时，开启浮点上下文自动保存与恢复，即</p>
</li>
</ul>
<pre><code class="C">cm_scs.scb.fpu.fpccr.bit.aspen = 1;
</code></pre>

<h2 id="_1">栈结构</h2>
<p>C语言一般将寄存器分为两部分：易失性的(volatile)和非易失性的(non-volatile)。
当使用到易失性(volatile)寄存器时不需要保存值和恢复；
当使用到非易失性(non-volatile)寄存器时需要保存值和恢复。</p>
<h3 id="volatile">易失性(volatile)栈结构</h3>
<p>ARMv6/v7-m的CPU，从线程模式进入中断模式时，会自动将易失性(volatile)寄存器保存到栈中。</p>
<pre><code class="C">            ------------------------
            | Prev Frame           |
            +------------+---------+
   sp+0x3C  |            |         |
   sp+0x38  | FPSCR      |         |
   sp+0x3C  | s15        |         |
   sp+0x38  | s14        |         |
   sp+0x34  | s13        |         |
   sp+0x30  | s12        |         |
   sp+0x2C  | s11        |         |
   sp+0x28  | s10        |         |
   sp+0x24  | s9         |   FP    |
   sp+0x20  | s8         |  Frame  |
   sp+0x3C  | s7         |         |
   sp+0x38  | s6         |         |
   sp+0x34  | s5         |         |
   sp+0x30  | s4         |         |
   sp+0x2C  | s3         |         |
   sp+0x28  | s2         |         |
   sp+0x24  | s1         |         |
   sp+0x20  | s0         |         |
            ------------------------
   sp+0x1C  | xpsr       |         |
   sp+0x18  | pc         |         |
   sp+0x14  | lr         |         |
   sp+0x10  | r12 (ip)   |  Basic  |
   sp+0x0C  | r3         |  Frame  |
   sp+0x08  | r2         |         |
   sp+0x04  | r1         |         |
   sp+0x00  | r0         |         |
            ------------------------
          易失性(volatile)寄存器栈结构
</code></pre>

<h3 id="non-volatile">非易失性(non-volatile)栈结构</h3>
<p>编译器能自动处理非易失性(non-volatile)寄存器，因此在同一个上下文中恢复环境，不需要
保存非易失性(non-volatile)寄存器。但当操作系统切换任务时，不同的任务的
非易失性(non-volatile)寄存器环境是不一样的，需要进行保存与恢复。</p>
<p>玄武OS在ARMv6/7-m平台上的非易失性(non-volatile)寄存器栈结构如下：</p>
<pre><code class="C">            -----------------------------
            |   Volatile stack Frame    |
            +-----------------+---------+
   sp+0x64  | s31             |         |
   sp+0x60  | s30             |         |
   sp+0x5C  | s29             |         |
   sp+0x58  | s28             |         |
   sp+0x54  | s27             |         |
   sp+0x50  | s26             |         |
   sp+0x4C  | s25             |   FP    |
   sp+0x48  | s24             |  Frame  |
   sp+0x44  | s23             |         |
   sp+0x40  | s22             |         |
   sp+0x3C  | s21             |         |
   sp+0x38  | s20             |         |
   sp+0x34  | s19             |         |
   sp+0x30  | s18             |         |
   sp+0x2C  | s17             |         |
   sp+0x28  | s16             |         |
            -----------------------------
   sp+0x24  | lr (EXC_RETURN) |         |
   sp+0x20  | r11 (fp)        |         |
   sp+0x1C  | r10 (sl)        |         |
   sp+0x18  | r9              |         |
   sp+0x14  | r8              |  Basic  |
   sp+0x10  | r7              |  Frame  |
   sp+0x0C  | r6              |         |
   sp+0x08  | r5              |         |
   sp+0x04  | r4              |         |
   sp+0x00  | CONTROL         |         |
            -----------------------------
          非易失性(non-volatile)寄存器栈结构

</code></pre>

<h3 id="setjmplongjmp">setjmp/longjmp的栈结构</h3>
<p>setjmp/longjmp是C标准库中定义的可跨越函数的"goto"，由于ARMv7-m3不存在浮点单元，因此
有些libc的实现都没有考虑浮点寄存器栈，因此玄武OS重新实现了这两个函数。栈结构如下：</p>
<pre><code class="C">            -----------------------------
   sp+0x64  | s31             |         |
   sp+0x60  | s30             |         |
   sp+0x5C  | s29             |         |
   sp+0x58  | s28             |         |
   sp+0x54  | s27             |         |
   sp+0x50  | s26             |         |
   sp+0x4C  | s25             |   FP    |
   sp+0x48  | s24             |  Frame  |
   sp+0x44  | s23             |         |
   sp+0x40  | s22             |         |
   sp+0x3C  | s21             |         |
   sp+0x38  | s20             |         |
   sp+0x34  | s19             |         |
   sp+0x30  | s18             |         |
   sp+0x2C  | s17             |         |
   sp+0x28  | s16             |         |
            -----------------------------
   sp+0x24  | r14 (lr)        |         |
   sp+0x20  | r13 (sp)        |         |
   sp+0x1C  | r11 (fp)        |         |
   sp+0x18  | r10 (sl)        |         |
   sp+0x14  | r9              |  Basic  |
   sp+0x10  | r8              |  Frame  |
   sp+0x0C  | r7              |         |
   sp+0x08  | r6              |         |
   sp+0x04  | r5              |         |
   sp+0x00  | r4              |         |
            -----------------------------
              setjmp/longjmp寄存器栈结构

</code></pre></div>
            </div>
        </div>

        <footer class="col-md-12">
            <hr>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script>
            var base_url = "../../..",
                shortcuts = {"help": 191, "next": 78, "previous": 80, "search": 83};
        </script>
        <script src="../../../js/base.js" defer></script>
        <script src="../../../search/main.js" defer></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="searchModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="searchModalLabel">搜索</h4>
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
                <p>
                    在这里所有文档：
                </p>
                <form>
                    <div class="form-group">
                        <input type="search" class="form-control" placeholder="搜索..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="keyboardModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="keyboardModalLabel">Keyboard Shortcuts</h4>
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>
